package com.wt.struggle.goodbye.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


/**
 * 微信支付
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String WEIXIN_ID = "";

    IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, WEIXIN_ID);

        api.handleIntent(getIntent(), this);


    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        int code = baseResp.errCode;
        Log.i("result", "==code===" + code);
        switch (code) {
            case 0:
                if (back != null) {
                    back.onPayBack(0);
                    finish();
                }

                break;

            case -1:

                if (back != null) {
                    back.onPayBack(-1);
                }

                finish();

                break;

            case -2:

                // 用户取消
                if (back != null) {
                    back.onPayBack(-2);
                }

                finish();
                break;
        }

    }


    private static OnPayBack back;

    public OnPayBack getBack() {
        return back;
    }

    public static void setBack(OnPayBack back) {
        WXPayEntryActivity.back = back;
    }

    public interface OnPayBack {
        void onPayBack(int code);
    }
}
